/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JEVEMeAddAccountBox.java
 *
 * Created on Jun 13, 2011, 3:55:17 PM
 */
package jeveme;

import org.jdesktop.application.Action;
import java.awt.CardLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JCheckBox;
import eveapi.EVEApiConnector;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import jeveme.datacontainers.*;
import java.util.ArrayList;

/**
 *
 * @author ruizr
 */
public class JEVEMeAddAccountBox extends javax.swing.JDialog {

    /** Creates new form JEVEMeAddAccountBox */
    public JEVEMeAddAccountBox(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        m_charactersInAccount = new ArrayList<EVECharacter>();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        characterSelectButtonGroup = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        apiDetailsPanel = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        userIDLabel = new javax.swing.JLabel();
        apiKeyLabel = new javax.swing.JLabel();
        userIDTextField = new javax.swing.JTextField();
        apiKeyTextField = new javax.swing.JTextField();
        fetchingDataPanel = new javax.swing.JPanel();
        fetchingLabel = new javax.swing.JLabel();
        errorDisplayPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        errorDisplayCancelButton = new javax.swing.JButton();
        errorDisplayImportButton = new javax.swing.JButton();
        errorDisplayPreviousButton = new javax.swing.JButton();
        showCharactersPanel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        characterListPanel = new javax.swing.JPanel();
        errorDisplayCancelButton1 = new javax.swing.JButton();
        errorDisplayImportButton1 = new javax.swing.JButton();
        errorDisplayPreviousButton1 = new javax.swing.JButton();
        importingCharPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jeveme.JEVEMeApp.class).getContext().getResourceMap(JEVEMeAddAccountBox.class);
        setTitle(resourceMap.getString("AddAccountBox.title")); // NOI18N
        setName("AddAccountBox"); // NOI18N
        setResizable(false);

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new java.awt.CardLayout());

        apiDetailsPanel.setBackground(resourceMap.getColor("apiKeyDetailsCard.background")); // NOI18N
        apiDetailsPanel.setName("apiKeyDetailsCard"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jeveme.JEVEMeApp.class).getContext().getActionMap(JEVEMeAddAccountBox.class, this);
        jButton1.setAction(actionMap.get("didPressNextButton")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("didPressCancelButton")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        userIDLabel.setLabelFor(userIDTextField);
        userIDLabel.setText(resourceMap.getString("userIDLabel.text")); // NOI18N
        userIDLabel.setName("userIDLabel"); // NOI18N

        apiKeyLabel.setLabelFor(apiKeyTextField);
        apiKeyLabel.setText(resourceMap.getString("apiKeyLabel.text")); // NOI18N
        apiKeyLabel.setName("apiKeyLabel"); // NOI18N

        userIDTextField.setText(resourceMap.getString("userIDTextField.text")); // NOI18N
        userIDTextField.setName("userIDTextField"); // NOI18N

        apiKeyTextField.setText(resourceMap.getString("apiKeyTextField.text")); // NOI18N
        apiKeyTextField.setName("apiKeyTextField"); // NOI18N

        javax.swing.GroupLayout apiDetailsPanelLayout = new javax.swing.GroupLayout(apiDetailsPanel);
        apiDetailsPanel.setLayout(apiDetailsPanelLayout);
        apiDetailsPanelLayout.setHorizontalGroup(
            apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, apiDetailsPanelLayout.createSequentialGroup()
                .addContainerGap(313, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(28, 28, 28)
                .addComponent(jButton2)
                .addGap(337, 337, 337))
            .addGroup(apiDetailsPanelLayout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addGroup(apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userIDLabel, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(apiKeyLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userIDTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(apiKeyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(288, Short.MAX_VALUE))
        );
        apiDetailsPanelLayout.setVerticalGroup(
            apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, apiDetailsPanelLayout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addGroup(apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userIDLabel)
                    .addComponent(userIDTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apiKeyLabel)
                    .addComponent(apiKeyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addGroup(apiDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        mainPanel.add(apiDetailsPanel, "apiKeyDetailsCard");

        fetchingDataPanel.setBackground(resourceMap.getColor("fetchingDataCard.background")); // NOI18N
        fetchingDataPanel.setName("fetchingDataCard"); // NOI18N

        fetchingLabel.setFont(resourceMap.getFont("fetchingLabel.font")); // NOI18N
        fetchingLabel.setText(resourceMap.getString("fetchingLabel.text")); // NOI18N
        fetchingLabel.setName("fetchingLabel"); // NOI18N

        javax.swing.GroupLayout fetchingDataPanelLayout = new javax.swing.GroupLayout(fetchingDataPanel);
        fetchingDataPanel.setLayout(fetchingDataPanelLayout);
        fetchingDataPanelLayout.setHorizontalGroup(
            fetchingDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, fetchingDataPanelLayout.createSequentialGroup()
                .addContainerGap(328, Short.MAX_VALUE)
                .addComponent(fetchingLabel)
                .addGap(63, 63, 63))
        );
        fetchingDataPanelLayout.setVerticalGroup(
            fetchingDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fetchingDataPanelLayout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addComponent(fetchingLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(129, Short.MAX_VALUE))
        );

        mainPanel.add(fetchingDataPanel, "fetchingDataCard");

        errorDisplayPanel.setBackground(resourceMap.getColor("errorDisplayCard.background")); // NOI18N
        errorDisplayPanel.setName("errorDisplayCard"); // NOI18N

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addContainerGap(247, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        errorDisplayCancelButton.setAction(actionMap.get("didPressCancelButton")); // NOI18N
        errorDisplayCancelButton.setText(resourceMap.getString("errorDisplayCancelButton.text")); // NOI18N
        errorDisplayCancelButton.setName("errorDisplayCancelButton"); // NOI18N

        errorDisplayImportButton.setText(resourceMap.getString("errorDisplayImportButton.text")); // NOI18N
        errorDisplayImportButton.setEnabled(false);
        errorDisplayImportButton.setName("errorDisplayImportButton"); // NOI18N

        errorDisplayPreviousButton.setAction(actionMap.get("didPressErrorDisplayPreviousButton")); // NOI18N
        errorDisplayPreviousButton.setText(resourceMap.getString("errorDisplayPreviousButton.text")); // NOI18N
        errorDisplayPreviousButton.setName("errorDisplayPreviousButton"); // NOI18N

        javax.swing.GroupLayout errorDisplayPanelLayout = new javax.swing.GroupLayout(errorDisplayPanel);
        errorDisplayPanel.setLayout(errorDisplayPanelLayout);
        errorDisplayPanelLayout.setHorizontalGroup(
            errorDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(errorDisplayPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(errorDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(errorDisplayPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, errorDisplayPanelLayout.createSequentialGroup()
                        .addComponent(errorDisplayPreviousButton)
                        .addGap(44, 44, 44)
                        .addComponent(errorDisplayImportButton)
                        .addGap(18, 18, 18)
                        .addComponent(errorDisplayCancelButton)))
                .addContainerGap())
        );
        errorDisplayPanelLayout.setVerticalGroup(
            errorDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(errorDisplayPanelLayout.createSequentialGroup()
                .addGroup(errorDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(errorDisplayPanelLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel2))
                    .addGroup(errorDisplayPanelLayout.createSequentialGroup()
                        .addContainerGap(27, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(9, 9, 9)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(errorDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(errorDisplayCancelButton)
                    .addComponent(errorDisplayImportButton)
                    .addComponent(errorDisplayPreviousButton))
                .addContainerGap())
        );

        mainPanel.add(errorDisplayPanel, "errorDisplayCard");

        showCharactersPanel.setBackground(resourceMap.getColor("showCharactersCard.background")); // NOI18N
        showCharactersPanel.setName("showCharactersCard"); // NOI18N

        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        characterListPanel.setName("characterListPanel"); // NOI18N
        characterListPanel.setLayout(new javax.swing.BoxLayout(characterListPanel, javax.swing.BoxLayout.PAGE_AXIS));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(characterListPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel8)
                .addGap(27, 27, 27)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(characterListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
        );

        errorDisplayCancelButton1.setAction(actionMap.get("didPressCancelButton")); // NOI18N
        errorDisplayCancelButton1.setName("errorDisplayCancelButton1"); // NOI18N

        errorDisplayImportButton1.setAction(actionMap.get("didPressImportButton")); // NOI18N
        errorDisplayImportButton1.setText(resourceMap.getString("errorDisplayImportButton1.text")); // NOI18N
        errorDisplayImportButton1.setName("errorDisplayImportButton1"); // NOI18N

        errorDisplayPreviousButton1.setAction(actionMap.get("didPressErrorDisplayPreviousButton")); // NOI18N
        errorDisplayPreviousButton1.setName("errorDisplayPreviousButton1"); // NOI18N

        javax.swing.GroupLayout showCharactersPanelLayout = new javax.swing.GroupLayout(showCharactersPanel);
        showCharactersPanel.setLayout(showCharactersPanelLayout);
        showCharactersPanelLayout.setHorizontalGroup(
            showCharactersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(showCharactersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(showCharactersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(showCharactersPanelLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, showCharactersPanelLayout.createSequentialGroup()
                        .addComponent(errorDisplayPreviousButton1)
                        .addGap(44, 44, 44)
                        .addComponent(errorDisplayImportButton1)
                        .addGap(18, 18, 18)
                        .addComponent(errorDisplayCancelButton1)))
                .addContainerGap())
        );
        showCharactersPanelLayout.setVerticalGroup(
            showCharactersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(showCharactersPanelLayout.createSequentialGroup()
                .addGroup(showCharactersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(showCharactersPanelLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel7))
                    .addGroup(showCharactersPanelLayout.createSequentialGroup()
                        .addContainerGap(76, Short.MAX_VALUE)
                        .addComponent(jLabel6)
                        .addGap(9, 9, 9)))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(showCharactersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(errorDisplayCancelButton1)
                    .addComponent(errorDisplayImportButton1)
                    .addComponent(errorDisplayPreviousButton1))
                .addContainerGap())
        );

        mainPanel.add(showCharactersPanel, "showCharactersCard");

        importingCharPanel.setName("importingCharCard"); // NOI18N

        javax.swing.GroupLayout importingCharPanelLayout = new javax.swing.GroupLayout(importingCharPanel);
        importingCharPanel.setLayout(importingCharPanelLayout);
        importingCharPanelLayout.setHorizontalGroup(
            importingCharPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 810, Short.MAX_VALUE)
        );
        importingCharPanelLayout.setVerticalGroup(
            importingCharPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        mainPanel.add(importingCharPanel, "importingCharCard");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents



    @Action
    public void didPressNextButton() {
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.next(mainPanel); //Goes to the fetching information card.
        FetchCharacterData fcd = new FetchCharacterData(cl);
        new Thread(fcd).start();
    }

    @Action
    public void didPressCancelButton() {
        dispose();
    }

    @Action
    public void didPressErrorDisplayPreviousButton() {
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "apiKeyDetailsCard");
    }
    
    @Action
    public void didPressImportButton() {
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        cl.show(mainPanel, "importingCharCard");
        for(int i=0; i < m_charactersInAccount.size(); i++) {
            EVECharacter ec = m_charactersInAccount.get(i);
            if(ec.getTracked()) {
                ec.updateCharacter(false);
                try { //update the portrait
                    ec.updateCharacterPortraitFromEVE();
                } catch(Exception e) {}
                
            }
            else {
                continue;
            }
        }
        MasterDataContainer mdc = JEVEMeApp.getApplication().getMasterDataContainer();
        mdc.addAccount(m_accountToBeAdded);
        
        dispose();
    }
    
    
    
    /**
     * SubClass for threading of fetching characters and parsing data.
     */
    class FetchCharacterData implements Runnable {
        
        private CardLayout cl;
        
        public FetchCharacterData(CardLayout layout) {
            cl = layout;
        }
        
        @Override
        public void run() {
            String characters_xml = EVEApiConnector.getCharacterListXML(userIDTextField.getText(), apiKeyTextField.getText());
        //Parse the Character List XML for display in the next card.
        try {
                    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = domFactory.newDocumentBuilder();
                    org.xml.sax.InputSource is = new org.xml.sax.InputSource();
                    is.setCharacterStream(new java.io.StringReader(characters_xml));
                    Document doc = builder.parse(is);
                    XPath xpath = XPathFactory.newInstance().newXPath();
                    
                    //Error and Row Expressions
                    XPathExpression error_expression = xpath.compile("//error");
                    XPathExpression characters_expression = xpath.compile("//rowset//row");
                    
                    Object error_result = error_expression.evaluate(doc, XPathConstants.NODESET);
                    Object characters_result = characters_expression.evaluate(doc, XPathConstants.NODESET);
                
                    NodeList error_nodes = (NodeList) error_result;
                    NodeList character_nodes = (NodeList) characters_result;
                    
                    //Check if we got an error from CCP.
                    if(error_nodes.getLength() > 0) {
                        cl.show(mainPanel, "errorDisplayCard");
                        return;
                    }
                    else { //If not, continue parsing.

                        m_accountToBeAdded = new EVEAccount(userIDTextField.getText(), apiKeyTextField.getText());
                        CredentialsLevel crl = EVEAccount.getAccountType(userIDTextField.getText(), apiKeyTextField.getText());
                        m_accountToBeAdded.setCredentialsLevel(crl);
                        
                        //Parse all the characters and place them into m_accountToBeAdded
                        EVECharacter ec_to_add;
                        if(character_nodes.getLength() > 0) {
                            for(int char_loop=0; char_loop<character_nodes.getLength(); char_loop++) {
                                
                                ec_to_add = new EVECharacter(m_accountToBeAdded);
                                Element row_element = (Element) character_nodes.item(char_loop);
                                String char_name = row_element.getAttribute("name"); 
                                String char_id = row_element.getAttribute("characterID");
                                String char_corp_name = row_element.getAttribute("corporationName");
                                String char_corp_id = row_element.getAttribute("corporationID");
                                
                                ec_to_add.setCharacterName(char_name);
                                ec_to_add.setCharacterID(char_id);
                                ec_to_add.setCorporationName(char_corp_name);
                                ec_to_add.setCorporationID(char_corp_id);
                                ec_to_add.setTracked(true);
                                //Add the character to the account.
                                m_accountToBeAdded.addCharacter(ec_to_add); 
                                
                            }
                        }        
                  
                        //Get all the characters and add them to the account.
                        m_charactersInAccount = m_accountToBeAdded.getCharacters();
                        CheckBoxEvent e = new CheckBoxEvent();
                        for(int i=0; i < m_charactersInAccount.size(); i++) {
                            EVECharacter ec = m_charactersInAccount.get(i);
                            JCheckBox jcb = new JCheckBox();
                            jcb.setSelected(true);
                            jcb.setText(ec.getCharacterName());
                            jcb.addItemListener(e);
                            characterListPanel.add(jcb);
                        }
                        
                        //Revalidate the characterListPanel
                        characterListPanel.validate();
                        
                        cl.show(mainPanel, "showCharactersCard");
                    }
        }
        catch(Exception e) {}
        }
        
    }
    
    class CheckBoxEvent implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            JCheckBox jcb = (JCheckBox) e.getItem();
            EVECharacter ec = m_accountToBeAdded.getCharacterByName(jcb.getText());
            ec.setTracked(!ec.getTracked()); //Flip the tracked variable
        }
    }



  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel apiDetailsPanel;
    private javax.swing.JLabel apiKeyLabel;
    private javax.swing.JTextField apiKeyTextField;
    private javax.swing.JPanel characterListPanel;
    private javax.swing.ButtonGroup characterSelectButtonGroup;
    private javax.swing.JButton errorDisplayCancelButton;
    private javax.swing.JButton errorDisplayCancelButton1;
    private javax.swing.JButton errorDisplayImportButton;
    private javax.swing.JButton errorDisplayImportButton1;
    private javax.swing.JPanel errorDisplayPanel;
    private javax.swing.JButton errorDisplayPreviousButton;
    private javax.swing.JButton errorDisplayPreviousButton1;
    private javax.swing.JPanel fetchingDataPanel;
    private javax.swing.JLabel fetchingLabel;
    private javax.swing.JPanel importingCharPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel showCharactersPanel;
    private javax.swing.JLabel userIDLabel;
    private javax.swing.JTextField userIDTextField;
    // End of variables declaration//GEN-END:variables

    private EVEAccount m_accountToBeAdded;
    private ArrayList<EVECharacter> m_charactersInAccount;
}
